/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Tela;

import Conexao.ConectaBD;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Thalyson
 */
public class Tela_Login extends javax.swing.JFrame {

    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    
    public Tela_Login() throws ClassNotFoundException {
        initComponents();
        setSize(425, 275);
        con = ConectaBD.conectabd();
        setLocationRelativeTo(null);
    }

        public void Logar() throws ClassNotFoundException {
        String sql = "Select *from login where Usuario = ? and Senha = ? and Nivel = ?";

        if(tf_usuario.getText().equals("")){
        JOptionPane.showMessageDialog(null, "Campo Usuário é Obrigatório", "Aviso", JOptionPane.WARNING_MESSAGE);
        return;
        }
        if(tf_senha.getText().equals("")){
        JOptionPane.showMessageDialog(null, "Campo Senha é Obrigatório", "Aviso", JOptionPane.WARNING_MESSAGE);
        return;
        }
        
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1, tf_usuario.getText());
            pst.setString(2, tf_senha.getText());
            pst.setString(3, (String) cb_nivel.getSelectedItem());

            rs = pst.executeQuery();

            if (rs.next()) {
                
                if(cb_nivel.getSelectedItem() == "ADMINISTRADOR")
                {
                    Tela_PrincipalADM tp = new Tela_PrincipalADM();
                    tp.setVisible(true);
                    dispose();
                }                
                else if(cb_nivel.getSelectedItem() == "ENCARREGADO")
                {
                    Tela_PrincipalEncarregado tps = new Tela_PrincipalEncarregado();
                    tps.setVisible(true);
                    dispose();
                }
                else if(cb_nivel.getSelectedItem() == "OPERADOR")
                {
                    Tela_PrincipalOperador tpo = new Tela_PrincipalOperador();
                    tpo.setVisible(true);
                    dispose();
                }
            } else {
                JOptionPane.showMessageDialog(null, "USUÁRIO e SENHA inválidos", "Aviso", JOptionPane.ERROR_MESSAGE);
            }
        } catch (SQLException error) {
            JOptionPane.showMessageDialog(null, error);
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cb_nivel = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        tf_usuario = new javax.swing.JTextField();
        tf_senha = new javax.swing.JPasswordField();
        jb_entrar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("E-KEY - Login");
        setBackground(new java.awt.Color(255, 255, 255));
        setResizable(false);
        getContentPane().setLayout(null);

        cb_nivel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        cb_nivel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "OPERADOR", "ENCARREGADO", "ADMINISTRADOR" }));
        cb_nivel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        cb_nivel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_nivelActionPerformed(evt);
            }
        });
        cb_nivel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cb_nivelKeyPressed(evt);
            }
        });
        getContentPane().add(cb_nivel);
        cb_nivel.setBounds(120, 150, 180, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("NÍVEL");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(120, 130, 35, 15);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("SENHA");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(120, 70, 41, 15);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("USUÁRIO");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(120, 10, 56, 15);

        tf_usuario.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tf_usuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_usuarioActionPerformed(evt);
            }
        });
        getContentPane().add(tf_usuario);
        tf_usuario.setBounds(120, 30, 180, 30);

        tf_senha.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tf_senha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_senhaKeyPressed(evt);
            }
        });
        getContentPane().add(tf_senha);
        tf_senha.setBounds(120, 90, 180, 30);

        jb_entrar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jb_entrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Finish  (1).png"))); // NOI18N
        jb_entrar.setText("ENTRAR");
        jb_entrar.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jb_entrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_entrarActionPerformed(evt);
            }
        });
        getContentPane().add(jb_entrar);
        jb_entrar.setBounds(150, 190, 120, 50);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/icone_atenas.png"))); // NOI18N
        getContentPane().add(jLabel4);
        jLabel4.setBounds(300, 30, 120, 140);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/icone_chave.png"))); // NOI18N
        getContentPane().add(jLabel6);
        jLabel6.setBounds(0, 40, 116, 115);

        jLabel5.setBackground(new java.awt.Color(255, 255, 255));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Fundo E-Key.png"))); // NOI18N
        getContentPane().add(jLabel5);
        jLabel5.setBounds(0, 0, 420, 250);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void cb_nivelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_nivelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cb_nivelActionPerformed

    private void cb_nivelKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cb_nivelKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                Logar();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Tela_Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_cb_nivelKeyPressed

    private void tf_usuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_usuarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_usuarioActionPerformed

    private void tf_senhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_senhaKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            try {
                Logar();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(Tela_Login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_tf_senhaKeyPressed

    private void jb_entrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_entrarActionPerformed
        try {
            Logar();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Tela_Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jb_entrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Tela_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Tela_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Tela_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Tela_Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Tela_Login().setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Tela_Login.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb_nivel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JButton jb_entrar;
    private javax.swing.JPasswordField tf_senha;
    private javax.swing.JTextField tf_usuario;
    // End of variables declaration//GEN-END:variables
}
